package nc.tb.consolidate.ui;

import nc.tb.consolidate.ConsolidateGroup;
import nc.tb.cube.model.MetaPlan;
import nc.tb.pub.tree.TbFCTreeModel;
import nc.tb.pub.util.DimTools;
import nc.ui.pub.beans.UIDialog;
import nc.ui.pub.beans.UIRefPane;

public class CubeGroupRefPane extends UIRefPane {

	private transient static String HTML_BEGIN = "<html>";
	private transient static String HTML_END = "</html>";
	private transient static String NEXT_LINE = "<br>";
	private transient static String BLANKE = "&nbsp;";
	
	private MetaPlan[] cubes = null;
	private StringBuffer textBuff = null;
	private StringBuffer htmlBuff = null;
	protected ICubeGroupRefParent m_parent = null;
	private ConsolidateGroup m_cg = null;
	
	public CubeGroupRefPane(ICubeGroupRefParent parent, ConsolidateGroup cg) {
		super();
		m_parent = parent;
		setEditable(false);
		setGroupObj(cg);
		refResize(true);
	}
	
	public void setGroupObj(ConsolidateGroup cg) {
		m_cg = cg;
		cubes = m_cg==null ? null : m_cg.getCubes();
		textBuff = null;
		htmlBuff = null;
//		setCubes(m_cg==null ? null : m_cg.getCubes());
	}
	
	public ConsolidateGroup getGroupObj() {
		return m_cg;
	}
	
	public boolean isMultiSize() {
		return !(cubes == null || cubes.length < 2);
	}
	
	public void setToolTipText(String txt) {
		super.setToolTipText(txt);
		getUITextField().setToolTipText(txt);
		getUIButton().setToolTipText(txt);
	}
	
	private void refResize(boolean isInit) {
		setText(getText());
		setToolTipText(getHtmlTextBuff().toString());
		if (m_parent == null)
			return;
		if (isMultiSize()) {
			setPreferredSize(m_parent.getMaxRefSize());
		}
		else {
			setPreferredSize(m_parent.getMinRefSize());
		}
		if (!isInit)
			m_parent.fireRefResize();
//		revalidate();
//		repaint();
	}
	
//	private void setCubes(MetaPlan[] cbs) {
//		if (cubes != null) {
//			cubes.clear();
//			cubes = null;
//		}
//		if (cbs != null) {
//			cubes = new ArrayList<MetaPlan>(Arrays.asList(cbs));
//		}
//		else {
//			cubes = new ArrayList<MetaPlan>();
//		}
//	}
	
//	private MetaPlan[] getCubes() {
//		if (cubes == null || cubes.isEmpty())
//			return null;
//		else {
//			MetaPlan[] cbs = cubes.toArray(new MetaPlan[0]);
//			SortTool.sortByCode(cbs);
//			return cbs;
//		}
//	}
	
	private StringBuffer getTextBuffer() {
		if (textBuff == null) {
			textBuff = new StringBuffer();
			if (cubes != null) {
				for (MetaPlan cube : cubes) {
					if (cube == null)
						continue;
					if (textBuff.length() > 0)
						textBuff.append(", ");
					textBuff.append(cube.getObjname());
				}
			}
//			System.out.println("create Ref text: "+textBuff.toString());
		}
		return textBuff;
	}
	
	private StringBuffer getHtmlTextBuff() {
		if (htmlBuff == null) {
			htmlBuff = new StringBuffer();
			htmlBuff.append(HTML_BEGIN);
			if (cubes != null) {
				for (MetaPlan cube : cubes) {
					if (cube == null)
						continue;
					if (htmlBuff.length() > HTML_BEGIN.length()) {
						htmlBuff.append(NEXT_LINE).append(BLANKE);
					}
					htmlBuff.append(cube.getObjcode()).append(BLANKE).append(BLANKE).append(cube.getObjname());
				}
			}
			if (htmlBuff.length() <= HTML_BEGIN.length())
				htmlBuff = new StringBuffer();
			else
				htmlBuff.append(HTML_END);
		}
		return htmlBuff;
	}
	
	public String getText() {
		return getTextBuffer().toString();
	}
	
	public void onButtonClicked() {
		CubeGroupRefUI dlg = new CubeGroupRefUI(this, m_cg);
		if (dlg.showModal() == UIDialog.ID_OK) {
			ConsolidateGroup selObj = dlg.getGroupObj();
			if (selObj != null && selObj != m_cg) {
				MetaPlan[] selCubes = selObj.getCubes();
				if (DimTools.isSameArray(selCubes, cubes, true))
					return;
				setGroupObj(selObj);
//				m_cg = selObj;
//				textBuff = null;
				refResize(false);
				if (m_parent != null)
					m_parent.fireRefValueChanged();
			}
		}
	}
	
	protected TbFCTreeModel getCubeTreeModel() {
		return m_parent==null ? null : m_parent.getCubeTreeModel();
	}
//	protected MetaPlan[] getAllCubes() {
//		return m_parent==null ? null : m_parent.getAllCubes();
//	}
	protected ConsolidateGroup[] getAllGroups() {
		return m_parent==null ? null : m_parent.getAllGroups();
	}
}
